<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="作业名称" prop="name">
              <el-input
                v-model="dataForm.name"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="作业要求" prop="description">
              <el-input
                v-model="dataForm.description"
                placeholder="请输入"
                :style="{ width: '100%' }"
                true
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4 }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件" prop="attachments">
              <UploadFz
                v-model="dataForm.attachments"
                :fileSize="5"
                sizeUnit="MB"
                :limit="9"
                buttonText="点击上传"
              >
              </UploadFz>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="完成标准" prop="completeType">
              <el-radio-group v-model="dataForm.completeType" size="small">
                <el-radio
                  v-for="(item, index) in completeTypeOptions"
                  :key="index"
                  :label="item.id"
                  :disabled="item.disabled"
                >
                  {{ item.fullName }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="6" v-if="dataForm.completeType == 'review'">
            <el-form-item label="考评设置" prop="hasScore">
              <el-switch
                v-model="dataForm.hasScore"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col
            :span="12"
            v-if="dataForm.completeType == 'review' && dataForm.hasScore"
          >
            <el-form-item label="满分" prop="score">
              <el-input-number
                v-model="dataForm.score"
                controls-position="right"
                placeholder="数字文本"
                :step="1"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="dataForm.completeType == 'review'">
            <el-form-item label="阅卷人" prop="reviewUsers">
              <user-select
                v-model="dataForm.reviewUsers"
                placeholder="请选择"
                clearable
                :multiple="true"
              >
              </user-select>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

export default {
  name: "HomeworkForm",
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        courseId: "",
        name: "",
        description: "",
        attachments: [],
        completeType: "submit",
        hasScore: 0,
        score: "",
        reviewUsers: [],
      },
      rules: {},
      completeTypeOptions: [{ fullName: "提交作业", id: "submit" }], //, {"fullName": "批阅通过", "id": "review"}
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
      this.dataForm.completeType = "submit";
    },
    init(courseId, id) {
      this.dataForm.courseId = courseId;
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsCourseHomework/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            if (res.data) {
              this.dataInfo(res.data);
            }
            this.loading = false;
          });
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/course/TemsCourseHomework",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", res.data);
            },
          });
        });
      } else {
        request({
          url: "/api/course/TemsCourseHomework/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", res.data);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.attachments = JSON.stringify(_data.attachments);
      _data.hasScore = parseInt(_data.hasScore);
      _data.reviewUsers = JSON.stringify(_data.reviewUsers);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.attachments = JSON.parse(_dataAll.attachments);
      _dataAll.hasScore = parseInt(_dataAll.hasScore);
      _dataAll.reviewUsers = JSON.parse(_dataAll.reviewUsers);
      this.dataForm = _dataAll;
    },
  },
};
</script>
